<?php
/**
 * @file $HeadURL: imagesource.php $
 * @author $Author: LiangJian (liangjian@haibao.com) $
 * @date $Date: 2014-12-31 下午6:03:25 $
 * @brief 
 */
namespace haibao\cms\data;

use haibao\frame\data\query\Condition;
use \haibao\cms\model\filter\ImageSource as ImageSourceFilter;

class ImageSource extends \haibao\cms\data\BaseMysql{

	public function __construct(){
		parent::__construct('\haibao\cms\model\data\ImageSource');
	}
	
	/**
	 * 添加图片来源
	 * @param \haibao\cms\model\data\ImageSource $model
	 */
	public function addSource($model){
		parent::add($model);
		
		if(\haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::SYNC_OLD)){
			$this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_ARTICLE );
			$sql = "insert into `picsm_pic_source`(name) values(%s) ";
			$this->query($sql, array($model->Name));
		}
	}
	
	/**
	 * 精确查找单条记录
	 */
	public function getSourceByName($name){
		$query = new ImageSourceFilter();
		$query->select(array(
			ImageSourceFilter::CONDITION_FIELD_NAME_ID,
			ImageSourceFilter::CONDITION_FIELD_NAME_NAME,
		));
		$query->where(ImageSourceFilter::CONDITION_FIELD_NAME_NAME, Condition::CONDITION_EQUAL, $name);
		return $this->getOne($query);
	}
	
	/**
	 * 根据图片来源名称快速联想
	 */
	public function getSourcesByName($name,$isReal = false){
	    if(empty($name)){
	    	$status = '';
			if(!$isReal){
				$status = 'where Status=1 ';
			} else {
				$status = 'where RealStatus=1';
			}
	      $sql = "select * from `ImageSource` ".$status." order by OrderNum DESC limit 20";
	    }else{
	    	$status = '';
			if(!$isReal){
				$status = 'Status=1 and ';
			} else {
				$status = 'RealStatus=1 and ';
			}
		  $sql = "select * from `ImageSource` where ".$status." `Name` like %s limit 10";
	    }
	    $result = $this->query($sql,array("%".$name."%"));
		$data = $tmp = array();
		while ($result->fetch_assoc()){
			$tmp['Id'] = $result->getData('Id');
			$tmp['Name'] = $result->getData('Name');
			array_push($data, $tmp);
		}
		
		return $data;
	}

}